package com.member.server.controller;

import com.member.server.domain.Result;
import com.member.server.domain.dto.MemAccInfoDTO;
import com.member.server.domain.dto.MemAccLogInfoDTO;
import com.member.server.domain.param.*;
import com.member.server.domain.vo.MemAccLogVO;
import com.member.server.domain.vo.MemAccVO;
import com.member.server.service.IAccountBizService;
import com.member.server.service.IAccountQueryService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author gaojx1993
 * @version 1.0
 * @description AccountController
 * @since 2023/7/11 15:57
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    private IAccountBizService accountBizService;

    @Resource
    private IAccountQueryService accountQueryService;

    @PostMapping("pay")
    public Result<Void> accountPay(@RequestBody AccountPayParam accountPayParam) {
        if (Objects.isNull(accountPayParam.getMemberId())) {
            return Result.fail("会员id不能为空");
        }
        if (Objects.isNull(accountPayParam.getPayAmount()) || accountPayParam.getPayAmount() < 0) {
            return Result.fail("支付金额不能为空，且必须大于等于0");
        }

        accountBizService.accountPay(accountPayParam);
        return Result.success();
    }

    @PostMapping("recharge")
    public Result<Void> accountRecharge(@RequestBody AccountRechargeParam accountRechargeParam) {
        if (Objects.isNull(accountRechargeParam.getAccountId())) {
            return Result.fail("钱包id不能为空");
        }
        if (Objects.isNull(accountRechargeParam.getRechargeAmount()) || accountRechargeParam.getRechargeAmount() <= 0) {
            return Result.fail("充值金额不能为空，且必须大于0");
        }
        accountBizService.accountRecharge(accountRechargeParam);
        return Result.success();
    }

    @PostMapping("refund")
    public Result<Void> accountRefund(@RequestBody AccountRefundParam accountRefundParam) {
        if (Objects.isNull(accountRefundParam.getRecordId())) {
            return Result.fail("支付流水id不能为空");
        }
        if (Objects.isNull(accountRefundParam.getRefundAmount()) || accountRefundParam.getRefundAmount() <= 0) {
            return Result.fail("退款金额不能为空，且必须大于0");
        }
        accountBizService.accountRefund(accountRefundParam);
        return Result.success();
    }

    @PostMapping("query/member-account")
    public Result<MemAccVO> queryMemberAccount(@RequestBody QueryParam queryParam) {
        if (Objects.isNull(queryParam.getId())) {
            return Result.fail("会员id不能为空");
        }
        MemAccInfoDTO memAccInfoDTO = accountQueryService.queryMemberAccount(queryParam);
        return Result.success(MemAccVO.copy(memAccInfoDTO));
    }

    @PostMapping("query/member-account-logs")
    public Result<MemAccLogVO> queryMemberAccountLogs(@RequestBody PagingQueryParam queryParam) {
        if (Objects.isNull(queryParam.getId())) {
            return Result.fail("会员id不能为空");
        }
        MemAccLogInfoDTO memAccLogInfoDTO = accountQueryService.queryMemberAccountLogs(queryParam);
        return Result.success(MemAccLogVO.copy(memAccLogInfoDTO));
    }
}
